package cn.com.cifi.mars.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
//import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.dto.ApprovedDetailDto;
import cn.com.cifi.mars.bean.dto.ProjectByStagesDto;
import cn.com.cifi.mars.bean.dto.VersionTypeCountDto;
import cn.com.cifi.mars.bean.vo.price.ApprovedDiffDetailVO;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.ApprovedStateCodeEnum;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.CollectTypeEnum;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.HrOrgTypeEunm;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.IsSuccessEnum;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.constant.LogTypeEnum;
import cn.com.cifi.mars.constant.NumberEnum;
import cn.com.cifi.mars.constant.OrgTypeEunm;
import cn.com.cifi.mars.constant.ProcessTypeEnum;
import cn.com.cifi.mars.constant.SalyTypeEnum;
import cn.com.cifi.mars.constant.StageStatusEnum;
import cn.com.cifi.mars.constant.VersionStatusEnum;
import cn.com.cifi.mars.constant.VersionTypeEunm;
import cn.com.cifi.mars.constant.isAssessmentVersionEnum;
import cn.com.cifi.mars.entity.ApprovalBill;
import cn.com.cifi.mars.entity.ApprovalBillDetail;
import cn.com.cifi.mars.entity.ApprovalShot;
import cn.com.cifi.mars.entity.ApprovalVersionCompare;
import cn.com.cifi.mars.entity.MdmStage;
import cn.com.cifi.mars.entity.PriceValueOrg;
import cn.com.cifi.mars.entity.PriceVersion;
import cn.com.cifi.mars.entity.common.ProjectByStages;
import cn.com.cifi.mars.entity.common.SysProcessConfig;
import cn.com.cifi.mars.mapper.mars.ApprovalBillDetailMapper;
import cn.com.cifi.mars.mapper.mars.ApprovalBillMapper;
import cn.com.cifi.mars.mapper.mars.ApprovalShotMapper;
import cn.com.cifi.mars.mapper.mars.ApprovalVersionCompareMapper;
import cn.com.cifi.mars.mapper.mars.ApprovedProjectMapper;
import cn.com.cifi.mars.mapper.mars.PriceValueOrgMapper;
import cn.com.cifi.mars.mapper.mars.PriceVersionMapper;
import cn.com.cifi.mars.mapper.mars.ProjectByStagesMapper;
import cn.com.cifi.mars.mapper.mars.SysProcessConfigMapper;
import cn.com.cifi.mars.service.PriceVersionService;
import cn.com.cifi.mars.service.ProjectBasicService;
import cn.com.cifi.mars.service.StageService;
import cn.com.cifi.mars.service.SubmitApprovedProjectService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.util.NumberUtils;
import cn.com.cifi.mars.util.StrUtils;
import cn.com.cifi.mars.util.UUIDUtils;

@Service
public class SubmitApprovedProjectServiceImpl extends ServiceImpl<ApprovedProjectMapper,PriceVersion> implements SubmitApprovedProjectService {

    private static final Logger logger = LoggerFactory.getLogger(SubmitApprovedProjectServiceImpl.class);
    @Autowired
    private ApprovedProjectMapper approvedProjectMapper;
    @Autowired
    private ApprovalBillMapper approvalBillMapper;
    @Autowired
    private ApprovalBillDetailMapper approvalBillDetailMapper;
    @Autowired
    private SysProcessConfigMapper sysProcessConfigMapper;
    @Autowired
    private PriceVersionMapper priceVersionMapper;
    @Autowired
    private ApprovalVersionCompareMapper approvalVersionCompareMapper;
    @Autowired
    private PriceValueOrgMapper priceValueOrgMapper;
    @Autowired
    private ApprovalShotMapper approvalShotMapper;
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    ProjectByStagesMapper projectByStagesMapper;
    @Autowired
    ProjectBasicService projectBasicService;
    @Autowired
    StageService stageService;

    private List<ProjectByStages> approvedStageDtoList;

    @Override
    public List<ApprovalVersionCompare> goodsValueMethod(String projectId) {
        //根据projectId查询需要审批的项目的分期
        List<PriceVersion> priceVersionList = approvedProjectMapper.selectApprovedProject(projectId);
        //根据projectId查询项目下所有分期的所有版本的个数
        List<VersionTypeCountDto> versionTypeCountDtoList = approvedProjectMapper.selectVersionTypeCount(projectId);
        //查询项目下分期个数
        approvedStageDtoList =  approvedProjectMapper.selectApprovedStageCount(projectId,HrOrgTypeEunm.ORG_32.getKey());
        //分期的个数
        int stageCount = 0;
        if (null != approvedStageDtoList){
            stageCount = approvedStageDtoList.size();
        }
        //将提交审批的版本存入集合 approvedVersion
        List<String> approvedVersions = new ArrayList<String>();
        if (CollectionUtils.isNotEmpty(priceVersionList)){
            for (PriceVersion priceVersion : priceVersionList) {
                approvedVersions.add(priceVersion.getVersionType());
            }
        }
        //遍历存有审批版本的集合 approvedVersion
        int castDecisionCount = 0; //投资版总数
        int strategyCount = 0;  //战规版总数
        int getPriceCount = 0;  //大定价版总数
        int dynamicCount = 0;   //动态版总数
        for (String versionType : approvedVersions) {
            if (VersionTypeEunm.INVESTMENT.getKey().equals(versionType)){
                castDecisionCount++;
            }else if(VersionTypeEunm.WARPLAN.getKey().equals(versionType)){
                strategyCount++;
            }else if(VersionTypeEunm.GETPRICE.getKey().equals(versionType)){
                getPriceCount++;
            }else if(VersionTypeEunm.DYNAMIC.getKey().equals(versionType)){
                dynamicCount++;
            }
        }
        
        List<ApprovalVersionCompare> resultList = new ArrayList<>();
        
        List<String> projectfIds = getProjectfIds(projectId);
        if (CollectionUtils.isNotEmpty(projectfIds)){
            for (String projectfId : projectfIds) {
            	//分析调整前的版本组合
                List<Map<String, Object>> dynamicValue = selectAdjustBefore(projectfId,CollectTypeEnum.ADJUST_BEFORE.getKey());
                getAdjustResultList(resultList, dynamicValue,CollectTypeEnum.ADJUST_BEFORE.getKey());
            
                //分析调整后的版本组合,如果调整后分期已被禁用，则不对比
                List<Map<String, Object>> dynamicValueTwo = selectAdjustBefore(projectfId,CollectTypeEnum.ADJUST_AFTER.getKey());
                getAdjustResultList(resultList, dynamicValueTwo,CollectTypeEnum.ADJUST_AFTER.getKey());
            
            }
            
        }
        
        //判断全盘货值概览需要展示的版本
        //1.全投资
        if (approvedVersions.size() == castDecisionCount){
            int castDecisionNum = 0;//投资版数量
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())){
                    //是否对比投资版
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        castDecisionNum ++;
                    }
                }
            }
            //统计的投资版个数与分期的个数是否相同
            if (castDecisionNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.INVESTMENT.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.CASTDECISION.getKey());
            }
            return resultList;
        }
        //2.全战规/（投资/战规）
        if (approvedVersions.size() == strategyCount || (castDecisionCount + strategyCount) == approvedVersions.size()){
            int castDecisionNum = 0;//投资版数量
            int strategyNum = 0;//战规版数量
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        castDecisionNum ++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())){
                    //是否对比战规版
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        strategyNum ++;
                    }
                }
            }
            //统计的投资版个数与分期的个数是否相同
            if (castDecisionNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.INVESTMENT.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.CASTDECISION.getKey());
            }
            //统计的战规版个数与分期的个数是否相同
            if (strategyNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.WARPLAN.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.WARPLAN.getKey());
            }
            return resultList;
        }
        //3.全大定价/（投资/大定价）/（战规/大定价）/（投资/战规/大定价）
        if (approvedVersions.size() == getPriceCount || (castDecisionCount + getPriceCount) == approvedVersions.size()
                || (strategyCount + getPriceCount) == approvedVersions.size() ||
                (castDecisionCount + strategyCount + getPriceCount) == approvedVersions.size()){
            int castDecisionNum = 0;//投资版数量
            int strategyNum = 0;//战规版数量
            int getPriceNum = 0;//大定价版数量
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        castDecisionNum ++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        strategyNum ++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.GETPRICE.getKey())){
                    //是否对比大定价
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        getPriceNum ++;
                    }
                }
            }
            //统计的投资版个数与分期的个数是否相同
            if (castDecisionNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.INVESTMENT.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.CASTDECISION.getKey());
            }
            //统计的战规版个数与分期的个数是否相同
            if (strategyNum == stageCount){
            	 List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.WARPLAN.getKey());
                 getResultList(resultList, priceVersions,CollectTypeEnum.WARPLAN.getKey());
            }
            //统计的大定价版个数与分期的个数是否相同
            if (getPriceNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.GETPRICE.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.GETPRICE.getKey());
            }
            return resultList;
        }
        //4.全动态/(投资/动态)/(战规/动态)/(大定价/动态)/(投资/战规/动态)/(投资/大定价/动态)/(战规/大定价/动态)/(投资/战规/大定价/动态)
        if (approvedVersions.size() == dynamicCount || (castDecisionCount + dynamicCount) == approvedVersions.size()
                || (strategyCount + dynamicCount) == approvedVersions.size() || (getPriceCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + strategyCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + getPriceCount + dynamicCount) == approvedVersions.size()
                || (strategyCount + getPriceCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + strategyCount + getPriceCount + dynamicCount) == approvedVersions.size()
                ){
            int castDecisionNum = 0;//投资版数量
            int strategyNum = 0;//战规版数量
            int getPriceNum = 0;//大定价版数量
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        castDecisionNum ++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        strategyNum ++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.GETPRICE.getKey())){
                    if (versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                            versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey())){
                        getPriceNum ++;
                    }
                }
            }

            //统计的投资版个数与分期的个数是否相同
            if (castDecisionNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.INVESTMENT.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.CASTDECISION.getKey());
            }
            //统计的战规版个数与分期的个数是否相同
            if (strategyNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.WARPLAN.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.WARPLAN.getKey());
            }
            //统计的大定价版个数与分期的个数是否相同
            if (getPriceNum == stageCount){
            	List<PriceVersion> priceVersions = selectVersionId(projectId,VersionTypeEunm.GETPRICE.getKey());
                getResultList(resultList, priceVersions,CollectTypeEnum.GETPRICE.getKey());
            }
            return resultList;
        }
        return null;
    }

    @Override
    public boolean stageStateInfo(String projectfId) {
        MdmStage mdmStage = stageService.selectById(projectfId);
        if (mdmStage != null) {
            String status = mdmStage.getStatus();
            if (StringUtils.isNotEmpty(status) && status.equals(StageStatusEnum.INACTIVE.getKey())) {
                return true;
            }
        } else {
            return true;
        }
        return false;
    }
    

    private void getAdjustResultList(List<ApprovalVersionCompare> resultList, List<Map<String, Object>> dynamicValue,String type) {
        if(CollectionUtils.isNotEmpty(dynamicValue)){
            for (Map<String, Object> objectMap : dynamicValue){
                if (String.valueOf(objectMap.get("type")).equals(OrgTypeEunm.ORG_STAGE.getKey())){
                    ApprovalVersionCompare approvalVersionCompare = new ApprovalVersionCompare();
                    String versionType = String.valueOf(objectMap.get("version"));
                    String versionNum = String.valueOf(objectMap.get("versionNum"));
                    String versionId = String.valueOf(objectMap.get("versionId"));
                    String stageId = String.valueOf(objectMap.get("orgId"));
                    String stageName = String.valueOf(objectMap.get("name"));
                    approvalVersionCompare.setVersionIdCompare(versionId);
                    approvalVersionCompare.setVersionTypeCompare(versionType);
                    approvalVersionCompare.setVersionNumCompare(versionNum);
                    approvalVersionCompare.setStageId(stageId);
                    approvalVersionCompare.setStageName(stageName);
                    approvalVersionCompare.setCollectType(type);
                    resultList.add(approvalVersionCompare);
                }
            }
        }
    }

    private void getResultList(List<ApprovalVersionCompare> resultList, List<PriceVersion> priceVersions,String type) {
        for (PriceVersion priceVersion : priceVersions) {
            ApprovalVersionCompare approvalVersionCompare = new ApprovalVersionCompare();
            approvalVersionCompare.setVersionIdCompare(priceVersion.getId());
            approvalVersionCompare.setVersionTypeCompare(priceVersion.getVersionType());
            approvalVersionCompare.setVersionNumCompare(priceVersion.getVersionNum());
            approvalVersionCompare.setStageId(priceVersion.getProjectfId());
            approvalVersionCompare.setStageName(priceVersion.getStageName());
            approvalVersionCompare.setCollectType(type);
            resultList.add(approvalVersionCompare);
        }
    }

    @Override
	public List<PriceVersion> selectVersionByBillId(String instanceId) {
		return approvedProjectMapper.selectVersionByBillId(instanceId);
	}
    
    //根据projectId查询需要审批的项目的分期
    @Override
    public List<PriceVersion> selectApprovedProject(String projectId) {
        return approvedProjectMapper.selectApprovedProject(projectId);
    }

    //根据projectId查询项目下所有分期的所有版本的个数
    @Override
    public List<VersionTypeCountDto> selectVersionTypeCount(String projectId) {
        return approvedProjectMapper.selectVersionTypeCount(projectId);
    }

    //根据projectId查询项目下所有审批通过的分期的个数
    @Override
    public List<ProjectByStages> selectApprovedStageCount(String projectId) {
        return approvedProjectMapper.selectApprovedStageCount(projectId,HrOrgTypeEunm.ORG_32.getKey());
    }

    //根据projectId查询需要展示的货值的id
    @Override
    public List<PriceVersion> selectVersionId(String projectId,String versionType) {
        if (VersionTypeEunm.INVESTMENT.getKey().equals(versionType)){
            return approvedProjectMapper.selectVersionId(projectId,versionType,isAssessmentVersionEnum.YES.getKey());
        }
        return approvedProjectMapper.selectVersionId(projectId,versionType,null);
    }

    //查询项目下是否有审批通过的stage
    @Override
    public int selectVersionType(String projectId,String versionType, String stageId) {
        if (VersionTypeEunm.INVESTMENT.getKey().equals(versionType)){
            return approvedProjectMapper.selectVersionType(projectId,versionType,stageId,isAssessmentVersionEnum.YES.getKey());
        }
        return approvedProjectMapper.selectVersionType(projectId,versionType,stageId,null);
    }
    
    @Override
	public List<PriceValueOrg> selectPriceValueOrgList(String projectId, String type) {
		return approvedProjectMapper.selectPriceValueOrgList(projectId,type,IsDeleteEnum.NO.getKey());
	}

    //查询项目下的分期
    @Override
    public List<String> getProjectfIds(String projectId) {
        return approvedProjectMapper.getProjectfIds(projectId);
    }

    //查询调整前(后)动态货值
    @Override
    public List<Map<String, Object>> selectAdjustBefore(String projectfId,String type) {
        //查询动态版货值
        List<Map<String, Object>> dynamicValue = new ArrayList<>();
        //先查询是否有明源已售或者已定价得版本
        dynamicValue = approvedProjectMapper.selectBeforeAdjustFirst(projectfId,type);
        if (CollectionUtils.isEmpty(dynamicValue)) {
            //如果明源没有,那就取动态版,大定价,战规中最新的作为全盘动态货值
            dynamicValue = approvedProjectMapper.selectBeforeAdjustSec(projectfId,type);
            if (CollectionUtils.isEmpty(dynamicValue)) {
                //战规前,就查询投资版最新得版本作为全盘动态货值
                dynamicValue = approvedProjectMapper.getCastDecision(projectfId,type);
            }
        }else {
        	dynamicValue = approvedProjectMapper.selectBeforeAdjustSec2(projectfId,type);
        }
        return dynamicValue;
    }

    //提交到OA审批时保存业务相关数据
    @Transactional
    @Override
    public ApprovalBill saveBusinessData(String projectId, String businessType, String flowKey, String account, String postCode, String orgCode,String description,String subject,String name) {
        ApprovalBill resApprovalBill;
        resApprovalBill = approvedProjectMapper.selectBillCheck(projectId, ApprovedStateCodeEnum.DRAFT.getKey(), ApprovedStateCodeEnum.REJECT.getKey(),ApprovedStateCodeEnum.RECALL.getKey(),ProcessTypeEnum.PRICE_MANAGEMENT.getKey());
        String id = null;
        if (null != resApprovalBill){
            id = resApprovalBill.getId();
        }else {
        	 //存在待编制或审批中的版本，不能提交审批
            List<PriceVersion> priceVersionList = approvedProjectMapper.checkVersion(projectId, VersionStatusEnum.NOTPREPARED.getKey(), VersionStatusEnum.APPROVED.getKey());
            if (CollectionUtils.isNotEmpty(priceVersionList)){
                throw new MsgException(CodeEnum.NOT_APPROVED);
            }

            //无审批编制中货值版本，不能提交审批
            List<PriceVersion> priceVersions = approvedProjectMapper.selectOrganizationVersion(projectId,VersionStatusEnum.ORGANIZATION.getKey());
            if(CollectionUtils.isEmpty(priceVersions)) {
            	throw new MsgException(CodeEnum.NOT_EXIST_APPROVED_VERSION);
            }
            //添加审批单
            resApprovalBill = new ApprovalBill();
            id = UUIDUtils.create();
            resApprovalBill.setId(id);
            resApprovalBill.setProjectId(projectId);
            resApprovalBill.setBusinessType(businessType);
            resApprovalBill.setFlowKey(flowKey);
            resApprovalBill.setUsercn(name);
            resApprovalBill.setAccount(account);
            resApprovalBill.setPostCode(postCode);
            resApprovalBill.setOrgCode(orgCode);
            resApprovalBill.setSubject(subject);
            resApprovalBill.setDescription(description);
            resApprovalBill.setCreatedTime(LocalDateTime.now());
            resApprovalBill.setState(ApprovedStateCodeEnum.NOT_LAUNCHED.getKey());
            approvalBillMapper.saveBusinessData(resApprovalBill);

            //添加审批单详情
            List<ApprovalBillDetail> list = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(priceVersions)){
                for (PriceVersion priceVersion : priceVersions) {
                    String detailId = UUIDUtils.create();
                    ApprovalBillDetail approvalBillDetail = new ApprovalBillDetail();
                    approvalBillDetail.setId(detailId);
                    approvalBillDetail.setBillId(id);

                    approvalBillDetail.setStageName(priceVersion.getStageName());
                    approvalBillDetail.setVersionType(priceVersion.getVersionType());
                    approvalBillDetail.setVersionId(priceVersion.getId());
                    approvalBillDetail.setVersionNum(priceVersion.getVersionNum());
                    list.add(approvalBillDetail);
                }
                approvalBillDetailMapper.insertList(list);
            }
            
            //添加审批对比版本
            List<ApprovalVersionCompare> versionCompareList = goodsValueMethod(projectId);
            if(CollectionUtils.isNotEmpty(versionCompareList)) {
            	for (ApprovalVersionCompare approvalVersionCompare : versionCompareList) {
                    String compareId = UUIDUtils.create();
                    approvalVersionCompare.setId(compareId);
                    approvalVersionCompare.setBillId(id);
                    approvalVersionCompare.setCreatedTime(LocalDateTime.now());

                    String projectfId = approvalVersionCompare.getStageId();
                    MdmStage mdmStage = stageService.selectById(projectfId);
                    String status = mdmStage.getStatus();
                    approvalVersionCompare.setStageStatus(status);
                }
            	approvalVersionCompareMapper.insertCompareList(versionCompareList);
            }
        }

        return resApprovalBill;
    }

    @Override
    public void updateApprovalState(String businessKey, String state, String instId,String taskId,LocalDateTime dateTime) {
        approvalBillMapper.updateApprovalState(businessKey,state,instId,taskId,dateTime);
        saveLog("提交到OA审批", "审批状态", state, IsSuccessEnum.SUCCESS.getKey());
    }

    @Override
    public List<PriceVersion> checkApprovedVersion(String projectId) {
        return approvedProjectMapper.checkVersion(projectId, VersionStatusEnum.NOTPREPARED.getKey(), VersionStatusEnum.APPROVED.getKey());
    }

    @Override
    public int checkApprovedVersionIsNull(String projectId) {
        int count = 0;
        List<PriceVersion> priceVersions = approvedProjectMapper.checkApprovedVersionIsNull(projectId);
        if (null != priceVersions && priceVersions.size() > 0){
            count = priceVersions.size();
        }
        return count;
    }

    @Override
    public ApprovalBill selectApprovalBillByKey(String businessKey) {
        return approvalBillMapper.selectApprovalBillByKey(businessKey);
    }

    //查询流程key
    @Override
    public String selectFlowKey(String regionId, String businessType) {
        SysProcessConfig sysProcessConfig = new SysProcessConfig();
        if (StringUtils.isNotBlank(regionId) && StringUtils.isNotBlank(businessType)){
            sysProcessConfig = sysProcessConfigMapper.selectFlowKey(regionId, businessType);
            if (null != sysProcessConfig){
                return sysProcessConfig.getKeyId();
            }
        }
        return null;
    }

    /**
     * 获取流程配置的keyId
     * @return
     */
    @Override
    public String getFlowKey(String businessType, String projectId) {

        // 查询项目区域
        List<String> str = projectBasicService.selectFullPath(projectId);
        String flowKey = null;
        String orgId = projectBasicService.selectRegionId(str, HrOrgTypeEunm.ORG_31.getKey());
        // 根据orgName和businessType查询流程key 签约计划 业务key=signing_plan,flowKey=keyId
        flowKey = selectFlowKey(orgId, businessType);
        if (org.apache.commons.lang3.StringUtils.isEmpty(flowKey)) {
            orgId = projectBasicService.selectRegionId(str, HrOrgTypeEunm.ORG_23.getKey());
            flowKey = selectFlowKey(orgId, businessType);
            if (org.apache.commons.lang3.StringUtils.isEmpty(flowKey)) {
                orgId = projectBasicService.selectRegionId(str, HrOrgTypeEunm.ORG_21.getKey());
                flowKey = selectFlowKey(orgId, businessType);
                if (org.apache.commons.lang3.StringUtils.isEmpty(flowKey)) {
                    orgId = projectBasicService.selectRegionId(str, HrOrgTypeEunm.ORG_20.getKey());
                    flowKey = selectFlowKey(orgId, businessType);
                }
            }
        }
        return flowKey;
    }

    //提交到OA审批校验
    @Override
    public List<PriceVersion> checkSubmitToOA(String projectId) {
        return approvedProjectMapper.checkVersion(projectId, VersionStatusEnum.APPROVED.getKey(), null);
    }

    //新建版本校验
    @Override
    public Boolean checkNewVersion(String projectfId) {
        PriceVersion priceVersion = priceVersionMapper.checkNewVersion(projectfId);
        if (null != priceVersion){
            String projectId = priceVersion.getProjectId();
            ApprovalBill approvalBill = approvalBillMapper.selectStateByProjectId(projectId);
            if (null != approvalBill){
                String state = approvalBill.getState();
                if (state.equals(ApprovedStateCodeEnum.DELETE.getKey()) || state.equals(ApprovedStateCodeEnum.ENDFLOW.getKey()) ||
                        state.equals(ApprovedStateCodeEnum.APPROVAL_SUCCESS.getKey())){
                    return true;
                }else {
                    return false;
                }
            }
        }
        return false;
    }

    @Override
    public List<String> selectVersionIdByKey(String key, String CollectType) {
        List<ApprovalVersionCompare> approvalVersionCompares = approvalVersionCompareMapper.selectVersionIdByKey(key, CollectType,VersionTypeEunm.DYNAMIC.getKey());
        List<String> idList = new ArrayList<>();
        String id = null;
        if (CollectionUtils.isNotEmpty(approvalVersionCompares)){
            for (ApprovalVersionCompare approvalVersionCompare : approvalVersionCompares) {
                id = approvalVersionCompare.getVersionIdCompare();
                idList.add(id);
            }
        }
        return idList;
    }

    @Override
    public void saveApprovedShot(List<String> ids, String type,String key) {
        List<ApprovalShot> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(ids)){
            for (String versionId : ids) {
                PriceValueOrg priceValueOrg = priceValueOrgMapper.selectValueOrgById(versionId, type);
                logger.info("SubmitApprovedProjectServiceImpl->saveApprovedShot:"+priceValueOrg);
                if (null != priceValueOrg){
                    try {
                        ApprovalShot approvalShot = new ApprovalShot();
                        approvalShot.setId(UUIDUtils.create());
                        approvalShot.setBillId(key);
                        approvalShot.setValueOrgId(priceValueOrg.getId());
                        approvalShot.setVersionId(priceValueOrg.getVersionId());
                        approvalShot.setPid(priceValueOrg.getPid());
                        approvalShot.setType(priceValueOrg.getType());
                        approvalShot.setOrgId(priceValueOrg.getOrgId());
                        approvalShot.setOrgCode(priceValueOrg.getOrgCode());
                        approvalShot.setOrgName(priceValueOrg.getOrgName());
                        approvalShot.setOpenTime(priceValueOrg.getOpenTime());
                        //新增
                        approvalShot.setOpenTimeActual(priceValueOrg.getOpenTimeActual());
                        approvalShot.setSupplyTime(priceValueOrg.getSupplyTime());
                        approvalShot.setSupplyTimeActual(priceValueOrg.getSupplyTimeActual());
                        approvalShot.setAdvanceSaleTime(priceValueOrg.getAdvanceSaleTime());
                        //新增
                        approvalShot.setAdvanceSaleTimeActual(priceValueOrg.getAdvanceSaleTimeActual());
                        approvalShot.setOpenSaleTime(priceValueOrg.getOpenSaleTime());
                        //新增
                        approvalShot.setOpenSaleTimeActual(priceValueOrg.getOpenSaleTimeActual());
                        approvalShot.setCappingTime(priceValueOrg.getCappingTime());
                        //新增
                        approvalShot.setCappingTimeActual(priceValueOrg.getCappingTimeActual());
                        approvalShot.setCompletedTime(priceValueOrg.getCompletedTime());
                        //新增
                        approvalShot.setCompletedTimeActual(priceValueOrg.getCompletedTimeActual());
                        approvalShot.setCarryOverTime(priceValueOrg.getCarryOverTime());
                        //新增
                        approvalShot.setCarryOverTimeActual(priceValueOrg.getCarryOverTimeActual());
                        approvalShot.setActualDeliveryTime(priceValueOrg.getActualDeliveryTime());
                        //新增
                        approvalShot.setActualDeliveryTimeActual(priceValueOrg.getActualDeliveryTimeActual());
                        approvalShot.setContractDeliveryTime(priceValueOrg.getContractDeliveryTime());
                        //新增
                        approvalShot.setContractDeliveryTimeActual(priceValueOrg.getContractDeliveryTimeActual());
                        approvalShot.setIsSupply(priceValueOrg.getIsSupply());
                        approvalShot.setIsSale(priceValueOrg.getIsSale());
                        approvalShot.setProProductCode(priceValueOrg.getProProductCode());
                        approvalShot.setProProductType(priceValueOrg.getProProductType());
                        approvalShot.setFreeType(priceValueOrg.getFreeType());
                        approvalShot.setFreeTypeName(priceValueOrg.getFreeTypeName());
                        approvalShot.setApartment(priceValueOrg.getApartment());
                        approvalShot.setProType(priceValueOrg.getProType());
                        approvalShot.setProTypeName(priceValueOrg.getProTypeName());
                        approvalShot.setBusinessType(priceValueOrg.getBusinessType());
                        approvalShot.setBusinessTypeName(priceValueOrg.getBusinessTypeName());
                        approvalShot.setProductCode(priceValueOrg.getProductCode());
                        approvalShot.setProductType(priceValueOrg.getProductType());
                        approvalShot.setAreaPart(priceValueOrg.getAreaPart());
                        approvalShot.setNum(priceValueOrg.getNum());
                        approvalShot.setRoomNum(priceValueOrg.getRoomNum());
                        approvalShot.setParkingNum(priceValueOrg.getParkingNum());
                        //新增
                        approvalShot.setForsaleParkingNum(priceValueOrg.getForsaleParkingNum());
                        approvalShot.setPrice(priceValueOrg.getPrice());
                        approvalShot.setPriceSold(priceValueOrg.getPriceSold());
                        approvalShot.setRoomSold(priceValueOrg.getRoomSold());
                        approvalShot.setAreaSold(priceValueOrg.getAreaSold());
                        //新增
                        approvalShot.setParkingSold(priceValueOrg.getParkingSold());
                        //新增
                        approvalShot.setPricePriced(priceValueOrg.getPricePriced());
                        //新增
                        approvalShot.setRoomPriced(priceValueOrg.getRoomPriced());
                        //新增
                        approvalShot.setAreaPriced(priceValueOrg.getAreaPriced());
                        //新增
                        approvalShot.setParkingPriced(priceValueOrg.getParkingPriced());

                        approvalShot.setAveragePriceParking(priceValueOrg.getAveragePriceParking());
                        approvalShot.setAveragePriceHouse(priceValueOrg.getAveragePriceHouse());
                        approvalShot.setSaleArea(priceValueOrg.getSaleArea());
                        approvalShot.setCommodityValue(priceValueOrg.getCommodityValue());
                        approvalShot.setHouseCommodityValue(priceValueOrg.getHouseCommodityValue());
                        approvalShot.setNotCommodityValue(priceValueOrg.getNotCommodityValue());
                        approvalShot.setPanoTotalRoomNum(priceValueOrg.getPanoTotalRoomNum());
                        approvalShot.setPanoTotalSaleArea(priceValueOrg.getPanoTotalSaleArea());
                        approvalShot.setPanoBatchRoomNum(priceValueOrg.getPanoBatchRoomNum());
                        approvalShot.setPanoBatchSaleArea(priceValueOrg.getPanoBatchSaleArea());
                        approvalShot.setPanoSalePark(priceValueOrg.getPanoSalePark());
                        approvalShot.setPanoTotalBuildArea(priceValueOrg.getPanoTotalBuildArea());
                        approvalShot.setPanoBatchBuildArea(priceValueOrg.getPanoBatchBuildArea());
                        approvalShot.setAreaTotalSaleArea(priceValueOrg.getAreaTotalSaleArea());
                        approvalShot.setAreaRoomNum(priceValueOrg.getAreaRoomNum());
                        approvalShot.setIsSplit(priceValueOrg.getIsSplit());
                        approvalShot.setIsParkingGroup(priceValueOrg.getIsParkingGroup());
                        approvalShot.setIsParking(priceValueOrg.getIsParking());
                        approvalShot.setIsDelete(priceValueOrg.getIsDelete());
                        approvalShot.setCreateBy(priceValueOrg.getCreateBy());
                        approvalShot.setCreateTime(priceValueOrg.getCreateTime());
                        approvalShot.setUpdateBy(priceValueOrg.getUpdateBy());
                        approvalShot.setUpdateTime(priceValueOrg.getUpdateTime());
                        list.add(approvalShot);
                    } catch (Exception e) {
                        logger.error("SubmitApprovedProjectServiceImpl->saveApprovedShot.error:"+e.getMessage(), e);
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(list)){
                approvalShotMapper.insertShotData(list);
            }
        }else {
            logger.info("SubmitApprovedProjectServiceImpl->saveApprovedShot:"+"没有快照信息");
        }
    }

    @Override
    public ApprovalShot selectShotInfo(String BussinesKey, String VersionId) {
        ApprovalShot approvalShot = null;
        if (StringUtils.isNotBlank(BussinesKey) && StringUtils.isNotBlank(VersionId)){
            approvalShot = approvalShotMapper.selectShotInfo(BussinesKey, VersionId);
        }
        return approvalShot;
    }

    @Override
    public void updateDescByBusinessKey(String bussinesKey, String description,String flowKey,String account,String name) {
        if (StringUtils.isNotBlank(bussinesKey) && StringUtils.isNotBlank(description)){
            approvalBillMapper.updateDescByBusinessKey(bussinesKey,description,flowKey,account,name);
        }
    }

    @Override
    public List<PriceVersion> selectPriceVersionByFId(String stageId, String key, List<String> states) {
    	return priceVersionMapper.selectPriceVersionByFId(stageId, VersionTypeEunm.DYNAMIC.getKey(), states, IsDeleteEnum.NO.getKey());
    }

    @Override
    public ApprovalBill selectApprovalBillByProjectId(String projectId,String bussinesType) {
        return approvalBillMapper.selectApprovalBillByProjectId(projectId,ApprovedStateCodeEnum.NOT_LAUNCHED.getKey(),bussinesType);
    }
   
    @Override
    public List<ApprovedDiffDetailVO>  differenceDetailResultData(String projectId,String businessKey,String type) {
        //从本系统进入审批页面时,查询本次对比的版本
        boolean isSubmitApprove = false;
    	List<ApprovalVersionCompare> approvalVersionCompares = new ArrayList<>();
    	if(StrUtils.isNotEmpty(businessKey)) {
    		 approvalVersionCompares = approvalVersionCompareMapper.selectByKey(businessKey);
            isSubmitApprove = true;
    	}else {
    		approvalVersionCompares = goodsValueMethod(projectId);
    	}
        List<ApprovalVersionCompare> afterList = new ArrayList<>();
        List<ApprovalVersionCompare> comparesList = new ArrayList<>();
        List<ApprovedDiffDetailVO> approvedDiffDetail = new ArrayList<>();
        
        for (ApprovalVersionCompare approvalVersionCompare : approvalVersionCompares) {
            String collectType = approvalVersionCompare.getCollectType();
            if (type.equals(collectType)){
            	comparesList.add(approvalVersionCompare);
            }else if (CollectTypeEnum.ADJUST_AFTER.getKey().equals(collectType)){
                afterList.add(approvalVersionCompare);
            }
        }
        
        for (ApprovalVersionCompare after : afterList) {
        	String stageId = after.getStageId();
        	ApprovalVersionCompare compare = getCompareVO(comparesList,stageId);
        	List<ApprovedDiffDetailVO> vo = mergeDataList(after,compare,type,isSubmitApprove);
			approvedDiffDetail.addAll(vo);
        }

        pricerVersionCompareInfo(projectId, type, approvedDiffDetail);

        return stageSort(approvedDiffDetail);
    }

    public void pricerVersionCompareInfo(String projectId, String type, List<ApprovedDiffDetailVO> approvedDiffDetail) {
        ApprovedDiffDetailVO project = new ApprovedDiffDetailVO();

        for(ApprovedDiffDetailVO vo : approvedDiffDetail) {
             if(vo.type.equals(OrgTypeEunm.ORG_STAGE.getKey())) {
                     vo.setPid(projectId);
                     project.setAfterRoomNum(NumberUtils.intADD(project.getAfterRoomNum(), vo.getAfterRoomNum()));
                     project.setAfterParkingNum(NumberUtils.intADD(project.getAfterParkingNum(),vo.getAfterParkingNum()));
                     project.setAfterArea(NumberUtils.BigDecimalADD(2, project.getAfterArea(), vo.getAfterArea()));
                     project.setAfterTotalPrice(NumberUtils.BigDecimalADD(2,project.getAfterTotalPrice(),vo.getAfterTotalPrice()));
                     project.setAfterHousePrice(NumberUtils.BigDecimalADD(2,project.getAfterHousePrice(),vo.getAfterHousePrice()));
                     project.setAfterParkingPrice(NumberUtils.BigDecimalADD(2,project.getAfterParkingPrice(),vo.getAfterParkingPrice()));

                     project.setBeforeRoomNum(NumberUtils.intADD(project.getBeforeRoomNum(),vo.getBeforeRoomNum()));
                     project.setBeforeParkingNum(NumberUtils.intADD(project.getBeforeParkingNum(),vo.getBeforeParkingNum()));
                     project.setBeforeArea(NumberUtils.BigDecimalADD(2,project.getBeforeArea(),vo.getBeforeArea()));
                     project.setBeforeTotalPrice(NumberUtils.BigDecimalADD(2,project.getBeforeTotalPrice(),vo.getBeforeTotalPrice()));
                     project.setBeforeHousePrice(NumberUtils.BigDecimalADD(2,project.getBeforeHousePrice(),vo.getBeforeHousePrice()));
                     project.setBeforeParkingPrice(NumberUtils.BigDecimalADD(2,project.getBeforeParkingPrice(),vo.getBeforeParkingPrice()));
             }
        }

        //添加项目层级
        ProjectByStagesDto dto = projectByStagesMapper.selectByOrgdepid(projectId);
        project.setId(projectId);
        project.setPid("0");
        project.setType(OrgTypeEunm.ORG_PRODUCTF.getKey());
        project.setOrgName(dto.getOrgname());

        project.setAfterHouseAvgPrice(NumberUtils.divide(project.getAfterHousePrice(), project.getAfterArea(), 4));
        project.setAfterParkingAvgPrice(NumberUtils.divide(project.getAfterParkingPrice(), project.getAfterParkingNum() == null ? null : new BigDecimal(project.getAfterParkingNum()),4));

        project.setBeforeHouseAvgPrice(NumberUtils.divide(project.getBeforeHousePrice(), project.getBeforeArea(), 4));
        project.setBeforeParkingAvgPrice(NumberUtils.divide(project.getBeforeParkingPrice(), project.getBeforeParkingNum() == null ? null : new BigDecimal(project.getBeforeParkingNum()), 4));

        //总货值金额差异（调整后-调整前的）
        if(project.getBeforeTotalPrice() != null) {
            BigDecimal TotalDiff = NumberUtils.subtract(project.getAfterTotalPrice(), project.getBeforeTotalPrice());
            project.setTotalDiff(TotalDiff);
            project.setTotalDiffPer(NumberUtils.divide(TotalDiff, project.getBeforeTotalPrice(), 4));
        }else {
        	 BigDecimal TotalDiff = NumberUtils.subtract(project.getAfterTotalPrice(),BigDecimal.ZERO);
             project.setTotalDiff(TotalDiff);
             project.setTotalDiffPer(NumberUtils.divide(TotalDiff,TotalDiff, 4));
        }
        //房源货值差异
        if(project.getBeforeHousePrice() != null) {
            BigDecimal priceDiff = NumberUtils.subtract(project.getAfterHousePrice(), project.getBeforeHousePrice());
            project.setPriceDiff(priceDiff);
            project.setPriceDiffPer(NumberUtils.divide(priceDiff, project.getBeforeHousePrice(), 4));
        }else {
        	 BigDecimal priceDiff = NumberUtils.subtract(project.getAfterHousePrice(), BigDecimal.ZERO);
             project.setPriceDiff(priceDiff);
             project.setPriceDiffPer(NumberUtils.divide(priceDiff, priceDiff, 4));
        }

        //房源均价差异
        if(project.getBeforeHouseAvgPrice() != null) {
            BigDecimal avgPriceDiff = NumberUtils.subtract(project.getAfterHouseAvgPrice(), project.getBeforeHouseAvgPrice());
            project.setAvgPriceDiff(avgPriceDiff);
            project.setAvgPriceDiffPer(NumberUtils.divide(avgPriceDiff, project.getBeforeHouseAvgPrice(), 4));
        }else {
        	BigDecimal avgPriceDiff = NumberUtils.subtract(project.getAfterHouseAvgPrice(), BigDecimal.ZERO);
            project.setAvgPriceDiff(avgPriceDiff);
            project.setAvgPriceDiffPer(NumberUtils.divide(avgPriceDiff, avgPriceDiff, 4));
        }

        //房源面积差异
        if(project.getBeforeArea() != null) {
            BigDecimal areaDiff = NumberUtils.subtract(project.getAfterArea(), project.getBeforeArea());
            project.setAreaDiff(areaDiff);
            project.setAreaDiffPer(NumberUtils.divide(areaDiff, project.getBeforeArea(), 4));
        }else {
        	 BigDecimal areaDiff = NumberUtils.subtract(project.getAfterArea(), BigDecimal.ZERO);
             project.setAreaDiff(areaDiff);
             project.setAreaDiffPer(NumberUtils.divide(areaDiff, areaDiff, 4));
        }

        //房源套数差异
        if(project.getBeforeRoomNum() != null) {
            Integer numDiff = NumberUtils.IntegerSubtract(project.getAfterRoomNum(),project.getBeforeRoomNum());
            project.setNumDiff(numDiff);
            project.setNumDiffPer(NumberUtils.divide(numDiff == null ? null : new BigDecimal(numDiff), project.getBeforeRoomNum() == null ? null : new BigDecimal(project.getBeforeRoomNum()), 4));
        }else {
        	Integer numDiff = NumberUtils.IntegerSubtract(project.getAfterRoomNum(),0);
            project.setNumDiff(numDiff);
            project.setNumDiffPer(NumberUtils.divide(numDiff == null ? null : new BigDecimal(numDiff),numDiff == null ? null : new BigDecimal(numDiff), 4));
        }

        //可售车位差异
        if(project.getBeforeParkingPrice() != null) {
            BigDecimal parkingPriceDiff = NumberUtils.subtract(project.getAfterParkingPrice(), project.getBeforeParkingPrice());
            project.setParkingPriceDiff(parkingPriceDiff);
            project.setParkingPriceDiffPer(NumberUtils.divide(parkingPriceDiff, project.getBeforeParkingPrice(), 4));
        }else {
        	BigDecimal parkingPriceDiff = NumberUtils.subtract(project.getAfterParkingPrice(),  BigDecimal.ZERO);
            project.setParkingPriceDiff(parkingPriceDiff);
            project.setParkingPriceDiffPer(NumberUtils.divide(parkingPriceDiff, parkingPriceDiff, 4));
        }

        //可售车位均价差异
        if(project.getBeforeParkingAvgPrice() !=null) {
        	BigDecimal parkingAvgPriceDiff = NumberUtils.subtract(project.getAfterParkingAvgPrice(),project.getBeforeParkingAvgPrice());
        	project.setParkingAvgPriceDiff(parkingAvgPriceDiff);
        	project.setParkingAvgPriceDiffPer(NumberUtils.divide(parkingAvgPriceDiff, project.getBeforeParkingAvgPrice(), 4));
        }else {
        	BigDecimal parkingAvgPriceDiff = NumberUtils.subtract(project.getAfterParkingAvgPrice(),BigDecimal.ZERO);
        	project.setParkingAvgPriceDiff(parkingAvgPriceDiff);
        	project.setParkingAvgPriceDiffPer(NumberUtils.divide(parkingAvgPriceDiff, parkingAvgPriceDiff, 4));
        }

        //可售车位 车位数差异
        if(project.getBeforeParkingNum() != null) {
            Integer parkingNumDiff = NumberUtils.subtract(project.getAfterParkingNum(),project.getBeforeParkingNum());
            project.setParkingNumDiff(parkingNumDiff);
            project.setParkingNumDiffPer(NumberUtils.divide(parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff), project.getBeforeParkingNum() == null ? null : new BigDecimal(project.getBeforeParkingNum()), 4));
        }else {
        	Integer parkingNumDiff = NumberUtils.subtract(project.getAfterParkingNum(),0);
            project.setParkingNumDiff(parkingNumDiff);
            project.setParkingNumDiffPer(NumberUtils.divide(parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff), parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff), 4));
        }

        approvedDiffDetail.add(project);
    }

    private ApprovalVersionCompare getCompareVO(List<ApprovalVersionCompare> comparesList,String stageId) {
    	ApprovalVersionCompare compareVO = null;
    	for(ApprovalVersionCompare compare : comparesList) {
    		if(compare.getStageId() != null && compare.getStageId().equals(stageId)) {
    			compareVO =  compare;
    		}
    	}
    	return compareVO;
    }
    
    public List<ApprovedDiffDetailVO> stageSort(List<ApprovedDiffDetailVO> resList) {
		List<ApprovedDiffDetailVO> lastList = Collections.synchronizedList(new ArrayList<>());
		List<ApprovedDiffDetailVO> sList = Collections.synchronizedList(new ArrayList<>());
		List<ApprovedDiffDetailVO> groupList = Collections.synchronizedList(new ArrayList<>());
		
		resList.forEach(x->{
			String typeTemp = x.getType();
			if(typeTemp.equals(OrgTypeEunm.ORG_STAGE.getKey())){
				sList.add(x);
            }else if(typeTemp.equals(OrgTypeEunm.ORG_GROUP.getKey())){
            	groupList.add(x);
            }else {
            	lastList.add(x);
            }
		});
		
		//分期排序
		if(CollectionUtils.isNotEmpty(sList)){
			sList.sort((a, b) -> {
                 String aCode = a.getOrgCode();
                 String bCode = b.getOrgCode();

                 if(StrUtils.isNotEmpty(aCode)) {
                	if(aCode.indexOf(".") >=0) {
                		aCode = aCode.substring(aCode.indexOf(".")+1, aCode.length());
                	}else {
                		aCode = "0";
                	}
                 }
                 
                 if(StrUtils.isNotEmpty(bCode)) {
                 	if(bCode.indexOf(".") >=0) {
                 		bCode = bCode.substring(bCode.indexOf(".")+1, bCode.length());
                 	}else {
                 		aCode = "0";
                 	}
                  }

                 return aCode == null ? 1 : bCode == null ? -1 : Integer.valueOf(aCode) - Integer.valueOf(bCode);
             });
			lastList.addAll(sList);
         }
			
		if(CollectionUtils.isNotEmpty(groupList)){
			groupList.sort((a, b) -> {
                String aName = a.getOrgName();
                String bName = b.getOrgName();

                String[] aStr = aName.split("组团");
                String[] bStr = bName.split("组团");

                if(aStr[0].equals(Constants.PARKING_NAME)) {
                    aStr[0] = "10000000";
                }
                if(bStr[0].equals(Constants.PARKING_NAME)){
                    bStr[0] = "10000000";
                }

                String aValue = NumberEnum.getKeyByValue(aStr[0]);
                String bValue = NumberEnum.getKeyByValue(bStr[0]);

                return aValue == null ? 1 : bValue == null ? -1 : Integer.valueOf(aValue) - Integer.valueOf(bValue);
            });
			
			lastList.addAll(groupList);
		}
		
		
		return lastList;
	}
    
    
    public List<ApprovedDiffDetailVO> mergeDataList(ApprovalVersionCompare afterData, ApprovalVersionCompare compare, String type,boolean isSubmitApprove) {
        //查看该分期是否被禁用
        String stageId = afterData.getStageId();
        boolean isStageSatateInactive = stageStateInfo(stageId);
        if (isSubmitApprove){
            String stageStatus = afterData.getStageStatus();
            if (StringUtils.isNotEmpty(stageStatus) && stageStatus.equals(StageStatusEnum.INACTIVE.getKey())){
                isStageSatateInactive = true;
            }else{
                isStageSatateInactive = false;
            }
        }

        List<ApprovedDiffDetailVO> approvedDiffDetailVO = new ArrayList<>();
        List<PriceValueOrg> afterList = new ArrayList<PriceValueOrg>();
        if (afterData != null){
           afterList = prepareCompareVersionData(afterData);
       }

        List<PriceValueOrg> compareList = new ArrayList<PriceValueOrg>();
    	 if (compare != null){
    	     compareList = prepareCompareVersionData(compare);
    	 }
    	 
    	 List<PriceValueOrg> mergeList = mergeOrgTree(afterList,compareList);
         
         for(PriceValueOrg dto : mergeList) {
         	ApprovedDiffDetailVO VO = new ApprovedDiffDetailVO();
         	String fullPath = dto.getFullNamePath();
         	
         	VO.setId(dto.getId());
         	VO.setPid(dto.getPid());
         	VO.setType(dto.getType());
         	VO.setOrgCode(dto.getOrgCode());
         	VO.setOrgName(dto.getOrgName());
         	VO.setIsParking(dto.getIsParking());
         	VO.setBusinessTypeName(dto.getBusinessTypeName());
         	VO.setFreeTypeName(dto.getFreeTypeName());
         	VO.setProTypeName(dto.getProTypeName());
         	
         	for(PriceValueOrg after : afterList) {
         		String afterFullPath = after.getFullNamePath();
                //如果分期被禁用，则没有调整后货值
                if (!isStageSatateInactive){
                    if(fullPath.equals(afterFullPath)) {
                        //调整后货值（不含未推售车位）
                        VO.setAfterRoomNum(after.getRoomNum());
                        VO.setAfterParkingNum(after.getParkingNum());
                        VO.setAfterArea(after.getSaleArea());
                        VO.setAfterHouseAvgPrice(NumberUtils.divide(after.getHouseCommodityValue(), after.getSaleArea(), 4));
                        VO.setAfterParkingAvgPrice(NumberUtils.divide(after.getNotCommodityValue(), after.getParkingNum()==null?null:new BigDecimal(after.getParkingNum()), 4));
                        VO.setAfterTotalPrice(NumberUtils.BigDecimalADD(0, after.getHouseCommodityValue(),after.getNotCommodityValue()));
                        VO.setAfterHousePrice(after.getHouseCommodityValue());
                        VO.setAfterParkingPrice(after.getNotCommodityValue());
                    }
                }
         	}
         	
         	if(CollectionUtils.isNotEmpty(compareList)) {
         		 for(PriceValueOrg before : compareList) {
             		String beforeFullPath = before.getFullNamePath();

             		if (StringUtils.isNotEmpty(type) &&
                            type.equals(CollectTypeEnum.ADJUST_BEFORE.getKey()) &&
                            isStageSatateInactive){
             		    continue ;
                    }

                    if (fullPath.equals(beforeFullPath)) {
                        //调整前货值（不含未推售车位）
                        VO.setBeforeRoomNum(before.getRoomNum());
                        VO.setBeforeParkingNum(before.getParkingNum());
                        VO.setBeforeArea(before.getSaleArea());
                        VO.setBeforeHouseAvgPrice(NumberUtils.divide(before.getHouseCommodityValue(), before.getSaleArea(), 4));
                        VO.setBeforeParkingAvgPrice(NumberUtils.divide(before.getNotCommodityValue(), before.getParkingNum() == null ? null : new BigDecimal(before.getParkingNum()), 4));
                        VO.setBeforeTotalPrice(NumberUtils.BigDecimalADD(0, before.getHouseCommodityValue(), before.getNotCommodityValue()));
                        VO.setBeforeHousePrice(before.getHouseCommodityValue());
                        VO.setBeforeParkingPrice(before.getNotCommodityValue());
                    }
             	}
         	}
         	approvedDiffDetailVO.add(VO);
         }
         
         if(CollectionUtils.isNotEmpty(compareList)) {
        	 for(ApprovedDiffDetailVO value : approvedDiffDetailVO) {
              	//总货值金额差异（调整后-调整前的）
              	BigDecimal totalDiff = NumberUtils.subtract(value.getAfterTotalPrice(), value.getBeforeTotalPrice());
              	value.setTotalDiff(totalDiff);
              	value.setTotalDiffPer(NumberUtils.divide(totalDiff, value.getBeforeTotalPrice(), 4));
              	//房源货值差异
              	BigDecimal priceDiff = NumberUtils.subtract(value.getAfterHousePrice(), value.getBeforeHousePrice());
              	value.setPriceDiff(priceDiff);
              	value.setPriceDiffPer(NumberUtils.divide(priceDiff, value.getBeforeHousePrice(), 4));
              	//房源均价差异
              	BigDecimal avgPriceDiff = NumberUtils.subtract(value.getAfterHouseAvgPrice(), value.getBeforeHouseAvgPrice());
              	value.setAvgPriceDiff(avgPriceDiff);
              	value.setAvgPriceDiffPer(NumberUtils.divide(avgPriceDiff, value.getBeforeHouseAvgPrice(), 4));
              	//房源面积差异
              	BigDecimal areaDiff = NumberUtils.subtract(value.getAfterArea(), value.getBeforeArea());
              	value.setAreaDiff(areaDiff);
              	value.setAreaDiffPer(NumberUtils.divide(areaDiff, value.getBeforeArea(), 4));
              	//房源套数差异
              	Integer numDiff = NumberUtils.IntegerSubtract(value.getAfterRoomNum(),value.getBeforeRoomNum());
              	value.setNumDiff(numDiff);
              	value.setNumDiffPer(NumberUtils.divide(numDiff == null ? null : new BigDecimal(numDiff), value.getBeforeRoomNum() == null ? null : new BigDecimal(value.getBeforeRoomNum()), 4));
              	//可售车位差异
              	BigDecimal parkingPriceDiff = NumberUtils.subtract(value.getAfterParkingPrice(), value.getBeforeParkingPrice());
              	value.setParkingPriceDiff(parkingPriceDiff);
              	value.setParkingPriceDiffPer(NumberUtils.divide(parkingPriceDiff, value.getBeforeParkingPrice(), 4));
              	//可售车位均价差异
              	BigDecimal parkingAvgPriceDiff = NumberUtils.subtract(value.getAfterParkingAvgPrice(),value.getBeforeParkingAvgPrice());
              	value.setParkingAvgPriceDiff(parkingAvgPriceDiff);
              	value.setParkingAvgPriceDiffPer(NumberUtils.divide(parkingAvgPriceDiff, value.getBeforeParkingAvgPrice(), 4));
              	//可售车位 车位数差异
              	Integer parkingNumDiff = NumberUtils.subtract(value.getAfterParkingNum(),value.getBeforeParkingNum());
              	value.setParkingNumDiff(parkingNumDiff);
              	value.setParkingNumDiffPer(NumberUtils.divide(parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff), value.getBeforeParkingNum() == null ? null : new BigDecimal(value.getBeforeParkingNum()), 4));
              }
         }else {
        	 for(ApprovedDiffDetailVO value : approvedDiffDetailVO) {
               	//总货值金额差异（调整后-调整前的）
               	BigDecimal totalDiff = NumberUtils.subtract(value.getAfterTotalPrice(),BigDecimal.ZERO);
               	value.setTotalDiff(totalDiff);
               	value.setTotalDiffPer(NumberUtils.divide(totalDiff,totalDiff, 4));
               	//房源货值差异
               	BigDecimal priceDiff = NumberUtils.subtract(value.getAfterHousePrice(),BigDecimal.ZERO);
               	value.setPriceDiff(priceDiff);
               	value.setPriceDiffPer(NumberUtils.divide(priceDiff,priceDiff,4));
               	//房源均价差异
               	BigDecimal avgPriceDiff = NumberUtils.subtract(value.getAfterHouseAvgPrice(),BigDecimal.ZERO);
               	value.setAvgPriceDiff(avgPriceDiff);
               	value.setAvgPriceDiffPer(NumberUtils.divide(avgPriceDiff,avgPriceDiff,4));
               	//房源面积差异
               	BigDecimal areaDiff = NumberUtils.subtract(value.getAfterArea(),BigDecimal.ZERO);
               	value.setAreaDiff(areaDiff);
               	value.setAreaDiffPer(NumberUtils.divide(areaDiff,areaDiff,4));
               	//房源套数差异
               	Integer numDiff = NumberUtils.IntegerSubtract(value.getAfterRoomNum(),0);
               	value.setNumDiff(numDiff);
               	value.setNumDiffPer(NumberUtils.divide(numDiff == null ? null : new BigDecimal(numDiff), numDiff == null ? null : new BigDecimal(numDiff),4));
               	//可售车位差异
               	BigDecimal parkingPriceDiff = NumberUtils.subtract(value.getAfterParkingPrice(),BigDecimal.ZERO);
               	value.setParkingPriceDiff(parkingPriceDiff);
               	value.setParkingPriceDiffPer(NumberUtils.divide(parkingPriceDiff,parkingPriceDiff,4));
               	//可售车位均价差异
               	BigDecimal parkingAvgPriceDiff = NumberUtils.subtract(value.getAfterParkingAvgPrice(),BigDecimal.ZERO);
               	value.setParkingAvgPriceDiff(parkingAvgPriceDiff);
               	value.setParkingAvgPriceDiffPer(NumberUtils.divide(parkingAvgPriceDiff,parkingAvgPriceDiff, 4));
               	//可售车位 车位数差异
               	Integer parkingNumDiff = NumberUtils.subtract(value.getAfterParkingNum(),0);
               	value.setParkingNumDiff(parkingNumDiff);
               	value.setParkingNumDiffPer(NumberUtils.divide(parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff), parkingNumDiff == null ? null : new BigDecimal(parkingNumDiff),4));
               }
         }
    	 
    	return approvedDiffDetailVO; 
	}

	private List<PriceValueOrg> mergeOrgTree(List<PriceValueOrg> adjustAfterList, List<PriceValueOrg> compareList) {
		 List<PriceValueOrg> mergeList = new ArrayList<>();
    	 List<PriceValueOrg> stageList = new ArrayList<>();
    	 List<PriceValueOrg> groupList = new ArrayList<>();
    	 List<PriceValueOrg> productList = new ArrayList<>();
    	 if (CollectionUtils.isNotEmpty(adjustAfterList)){
           for(PriceValueOrg after : adjustAfterList) {
               if(after.getType().equals(OrgTypeEunm.ORG_STAGE.getKey())) {
                   stageList.add(after);
               }else if(after.getType().equals(OrgTypeEunm.ORG_GROUP.getKey())) {
                   groupList.add(after);
               }else if(after.getType().equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
                   productList.add(after);
               }

           }
       }

    	 if (CollectionUtils.isNotEmpty(adjustAfterList)) {
           for (PriceValueOrg compare : compareList) {
               if (compare.getType().equals(OrgTypeEunm.ORG_STAGE.getKey())) {
                   stageList.add(compare);
               } else if (compare.getType().equals(OrgTypeEunm.ORG_GROUP.getKey())) {
                   groupList.add(compare);
               } else if (compare.getType().equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
                   productList.add(compare);
               }
           }
       }
    	 
    	 //去重
    	 stageList = stageList.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PriceValueOrg::getOrgName))), ArrayList::new));
    	
    	 groupList = groupList.stream().collect(
    			 Collectors.collectingAndThen(
    					 Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PriceValueOrg::getOrgName))), ArrayList::new));
    	
    	 productList = productList.stream().collect(
    			 Collectors.collectingAndThen(
    					 Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PriceValueOrg::getFullNamePath))), ArrayList::new));
	 
    	 
    	 
    	 for(PriceValueOrg p : productList) {
    		 String fullPath = p.getFullNamePath();
    		 for(PriceValueOrg g : groupList) {
    			 String orgName = g.getOrgName();
    			 if(fullPath.indexOf(orgName) != -1) {
    				 p.setPid(g.getId());
    				 break;
    			 }
    		 }
    	 }
    	 
    	 for(PriceValueOrg g : groupList) {
    		 String fullPath = g.getFullNamePath();
    		 for(PriceValueOrg s : stageList) {
    			 String orgName = s.getOrgName();
    			 if(fullPath.indexOf(orgName) != -1) {
    				 g.setPid(s.getId());
    				 break;
    			 }
    		 }
    	 }
    	 
    	 mergeList.addAll(stageList);
    	 mergeList.addAll(groupList);
    	 mergeList.addAll(productList);
    	 
		 return mergeList;
	}

	private List<PriceValueOrg> prepareCompareVersionData(ApprovalVersionCompare approvalVersionCompare) {		 
		List<PriceValueOrg> orgData = Collections.synchronizedList(new ArrayList<>());
    	 if(approvalVersionCompare != null) {
        	 String versionId = approvalVersionCompare.getVersionIdCompare();
        	 String versionType = approvalVersionCompare.getVersionTypeCompare();
        	 List<PriceValueOrg> orgTreeData = approvedProjectMapper.selectPriceValueOrg(versionId,IsDeleteEnum.NO.getKey());
        	 if(versionType.equals(VersionTypeEunm.INVESTMENT.getKey())) {
        		 //去除批次层级，重组数据
        		 orgData = collectData(orgTreeData);
        	 }else {
        		 orgData = handleData(orgTreeData);
        	 }
        	 orgData = setOrgFullpath(orgData,"0");
        	 
        	 for(PriceValueOrg org : orgData) {
        		 if(org.getType().equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
        			 org.setFullNamePath(org.getFullNamePath()+"/"+org.getBusinessTypeName()+"/"+org.getFreeTypeName()+"/"+org.getProTypeName());
        			 String proProductCode = org.getProProductCode();
        			 if(StrUtils.isNotEmpty(proProductCode) && proProductCode.equals("139")) {
        				 org.setIsParking(1);
        			 }else {
        				 org.setIsParking(0);
        			 }
        		 }
        	 }
        	 for(PriceValueOrg org : orgData) {
        		 if(org.getType().equals(OrgTypeEunm.ORG_GROUP.getKey())) {
        			 collectData(orgData,org);
        		 }
        	 }
        	 
    	 }
    	 
    	 return orgData;
    }
	
	 private void collectData(List<PriceValueOrg> resList,PriceValueOrg org) {
			List<PriceValueOrg> childList = getChildDataList(resList,org.getId());
			collectChildData(childList,org);
	 }
		
	private void collectChildData(List<PriceValueOrg> childList, PriceValueOrg org) {
		if(CollectionUtils.isNotEmpty(childList)) {
			for(PriceValueOrg org2 : childList) {
				org.setSaleArea(NumberUtils.BigDecimalADD(4,org.getSaleArea(),org2.getSaleArea()));
			}
		}
		
	}
		
	private List<PriceValueOrg> getChildDataList(List<PriceValueOrg> resList, String id) {
		List<PriceValueOrg> dataList = Collections.synchronizedList(new ArrayList<>());
		if(CollectionUtils.isNotEmpty(resList)) {
			for(PriceValueOrg obj : resList) {
				if(obj.getPid().equals(id)) {
					dataList.add(obj);
				}
			}
		}
		return dataList;
	}
	
	private List<PriceValueOrg> handleData(List<PriceValueOrg> orgTreeData) {
		 List<PriceValueOrg> resList = new ArrayList<>();
		for(PriceValueOrg org : orgTreeData) {
			if(org.getType().equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
				setProductData(resList, org);
			}else if(org.getType().equals(OrgTypeEunm.ORG_STAGE.getKey())) {
				org.setOrgName(org.getStageName());
				resList.add(org);
			}else {
				org.setSaleArea(null);
				resList.add(org);
			}
		}
		return resList;
	}

	private void setProductData(List<PriceValueOrg> resList, PriceValueOrg org) {
		List<ApprovedDetailDto> list = approvedProjectMapper.selectApprovedDetailDto(org.getVersionId(),org.getId(),IsDeleteEnum.NO.getKey());
		if(CollectionUtils.isNotEmpty(list)) {
			for(ApprovedDetailDto dto : list) {
				PriceValueOrg orgData  = new PriceValueOrg();
				orgData.setId(UUIDUtils.create());
				orgData.setPid(org.getPid());
				orgData.setOrgId(org.getOrgId());
				orgData.setOrgCode(org.getOrgCode());
				orgData.setOrgName(org.getOrgName());
				orgData.setProProductCode(org.getProProductCode());
				orgData.setProProductType(org.getProProductType());
				orgData.setType(OrgTypeEunm.ORG_PRODUCT.getKey());
				
				orgData.setBusinessType(dto.getBusinessType());
				orgData.setBusinessTypeName(dto.getBusinessTypeName());
				orgData.setFreeType(dto.getFreeType());
				orgData.setFreeTypeName(dto.getFreeTypeName());
				orgData.setProType(dto.getProType());
				orgData.setProTypeName(dto.getProTypeName());
				orgData.setRoomNum(dto.getRoomNum());
				orgData.setParkingNum(dto.getParkingNum());
				orgData.setForsaleParkingNum(dto.getForsaleParkingNum());
				orgData.setSaleArea(dto.getSaleArea());
				orgData.setCommodityValue(dto.getCommodityValue());
				orgData.setHouseCommodityValue(dto.getHouseCommodityValue());
				orgData.setNotCommodityValue(dto.getNotCommodityValue());
				resList.add(orgData);
			}
		}
	}

	private List<PriceValueOrg> setOrgFullpath(List<PriceValueOrg> OrgTree, String pid) {
		String fullPathName ="";
		for(PriceValueOrg dto : OrgTree) {
			if(dto.getId().equals(pid)) {
				fullPathName= dto.getFullNamePath();
			}
		}
		for(PriceValueOrg dto : OrgTree) {
			if(dto.getPid().equals(pid)) {
				dto.setFullNamePath(fullPathName+"/"+dto.getOrgName());
				setOrgFullpath(OrgTree,dto.getId());
			}
		}
		return OrgTree;
	}
    
    // 处理投资版数据
	private List<PriceValueOrg> collectData( List<PriceValueOrg> dataList) {
		for(PriceValueOrg objOrg : dataList) {
			if(objOrg.getIsParking() != null && objOrg.getIsParking().intValue() == 1) {
   			 	objOrg.setSaleArea(null);
			 }
	   		 if(objOrg.getType().equals(OrgTypeEunm.ORG_GROUP.getKey()) ) {
	   			objOrg.setSaleArea(null);
			 }
   	 	}
		//数据重组（将批次的子级放到组团下面并移除批次层级）
		dataList = regroupData(dataList);
		List<PriceValueOrg> resList = new ArrayList<PriceValueOrg>();
		List<String> groupIds = new ArrayList<String>();
		Map<String, List<PriceValueOrg>> groupMap = new HashMap<String, List<PriceValueOrg>>();
		dataList.forEach(x->{
			if(x.getType().equals(OrgTypeEunm.ORG_GROUP.getKey())) {
				groupIds.add(x.getId());
				resList.add(x);
			}else if(x.getType().equals(OrgTypeEunm.ORG_STAGE.getKey())) {
				x.setOrgName(x.getStageName());
				resList.add(x);
			}
		});
		
		for(String gId : groupIds) {
			List<PriceValueOrg> list = new ArrayList<PriceValueOrg>();
			for(PriceValueOrg org : dataList) {
				if(org.getPid().equals(gId)) {
					if(org.getBusinessType().equals(SalyTypeEnum.SALABLE.getKey()) || org.getBusinessType().equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getKey())) {
						list.add(org);
					}
				}
			}
			
			for(PriceValueOrg org : list) {
				org.setOrgNames(org.getOrgName()+"/"+org.getBusinessTypeName()+"/"+org.getFreeTypeName()+"/"+org.getProTypeName());
			}
			
			groupMap.put(gId, list);
			
			//去重
			list = list.stream().collect(
	                Collectors.collectingAndThen(
	                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PriceValueOrg::getOrgNames))), ArrayList::new));
			
			for(PriceValueOrg price : list) {
				String id = price.getId();
				String orgNames = price.getOrgNames();
				for(Map.Entry<String, List<PriceValueOrg>> entry : groupMap.entrySet()){
					List<PriceValueOrg> temp = entry.getValue();
					for(PriceValueOrg value : temp) {
						if(orgNames.equals(value.getOrgNames())) {
							if(id.equals(value.getId())) {
								continue;
							}
							price.setRoomNum(NumberUtils.intADD(price.getRoomNum(), value.getRoomNum()));
							price.setParkingNum(NumberUtils.intADD(price.getParkingNum(), value.getParkingNum()));
							price.setForsaleParkingNum(NumberUtils.intADD(price.getForsaleParkingNum(), value.getForsaleParkingNum()));
							price.setSaleArea(NumberUtils.BigDecimalADD(4, price.getSaleArea(), value.getSaleArea()));
							price.setCommodityValue(NumberUtils.BigDecimalADD(4, price.getCommodityValue(), value.getCommodityValue()));
							price.setHouseCommodityValue(NumberUtils.BigDecimalADD(4, price.getHouseCommodityValue(), value.getHouseCommodityValue()));
							price.setNotCommodityValue(NumberUtils.BigDecimalADD(4, price.getNotCommodityValue(), value.getNotCommodityValue()));
						}
					}
		        }
			}
			resList.addAll(list);
		}
		return resList;
	}
	
	private List<PriceValueOrg> regroupData( List<PriceValueOrg> dataList) {
		Map<String, String> map = new HashMap<String, String>();
		for(PriceValueOrg value : dataList) {
			String type = value.getType();
			if(type.equals(OrgTypeEunm.ORG_BATCH.getKey())) {
				String bId = value.getId();
				String bPid = value.getPid();
				map.put(bId, bPid);
			}
		}
		
		dataList = dataList.stream().filter(e -> !OrgTypeEunm.ORG_BATCH.getKey().equals(e.getType())).collect(Collectors.toList());
		
		for(PriceValueOrg value : dataList) {
			String pid = value.getPid();
			for(Map.Entry<String, String> entry : map.entrySet()){
	            if(entry.getKey().equals(pid)) {
	            	value.setPid(entry.getValue());
	            }
	        }
		}
		return dataList;
	}
	
	 private boolean saveLog(String module,String title,String content,String isSuccess) {
       SysLogBO bo = new SysLogBO();
       bo.setEvent(LogEventEnum.BROWSE);
       bo.setSource(LogSrcEnum.PC);
       bo.setModule(module);
       bo.setTitle(title);
       bo.setContent(content);
       bo.setType(LogTypeEnum.ONE.getKey());
       bo.setIsSuccess(isSuccess);
       boolean flag = sysLogService.add(bo,"admin");
       return flag;
   }

	
}
